package com.view.cn.mvprxjava.utils;

import android.graphics.Point;

import java.util.ArrayList;

/**
 * 五子棋，人机算法
 * Created by Zengxiaoping on 2016/12/15.
 */

public class AIUtils {

	public static final int MAX_LINE_NUMBER = 10;      //棋盘最大行列数
	public static final int MAX_NUMBER_IN_LINE = 5;    //五子连珠游戏结束

	/**
	 * 计算，机器落子的点
	 *
	 * @param point         用户落子的点
	 * @param dataListWhite 用户落子的点的集合
	 * @param dataListBlack 机器落子的点的集合
	 * @return 返回得分最大的点
	 */
	public static Point getPoint(Point point, ArrayList<Point> dataListWhite, ArrayList<Point> dataListBlack) {

		//进攻型得分
		ResultPoint defensivePoint = PieceDefensiveUtils.getDefensivePoint(point, dataListWhite, dataListBlack);
		//防御性得分
		ResultPoint offensivePoint = PieceDefensiveUtils.getDefensivePoint(point, dataListBlack, dataListWhite);

		ResultPoint resultPoint = defensivePoint.scoreNumber > offensivePoint.scoreNumber ? defensivePoint : offensivePoint;
		return resultPoint.point;
	}


	/**
	 * 记录点，和分数
	 */
	public static class ResultPoint {

		/**
		 * 定义分数线标准:电脑根据分数来取决下子位置,落子点是分数最高的那个点
		 * 自己       对手
		 * ------------------------------
		 * 活4:100   活4:95
		 * 死4:90    死4:85
		 * 活3:80    活3:75
		 * 死3:70    死3:65
		 * 活2:60    活2:55
		 * 死2:50    死2:45
		 * 活1:40    活1:35
		 * 死1:30    死1:25
		 */

		public Point point;
		public int scoreNumber;

		/**
		 * 计算该点得分
		 *
		 * @param leftExistPoint  左边堵死？
		 * @param rightExistPoint 右边堵死？
		 * @param sumNumber       连成一线的棋子数
		 * @param idUser     	   是用户？
		 * @return 返回得分
		 */
		public static int getScoreNumber(boolean leftExistPoint, boolean rightExistPoint, int sumNumber, boolean idUser) {
			switch (sumNumber) {
				case 5:
					if (!leftExistPoint && !rightExistPoint) {
						return !idUser ? 115 : 120;
					} else if (!leftExistPoint || !rightExistPoint) {
						return !idUser ? 105 : 110;
					}
					break;
				case 4:
					if (!leftExistPoint && !rightExistPoint) {
						return !idUser ? 95 : 100;
					} else if (!leftExistPoint || !rightExistPoint) {
						return !idUser ? 85 : 90;
					}
					break;
				case 3:
					if (!leftExistPoint && !rightExistPoint) {
						return !idUser ? 75 : 80;
					} else if (!leftExistPoint || !rightExistPoint) {
						return !idUser ? 65 : 70;
					}
					break;
				case 2:
					if (!leftExistPoint && !rightExistPoint) {
						return !idUser ? 55 : 60;
					} else if (!leftExistPoint || !rightExistPoint) {
						return !idUser ? 45 : 50;
					}
					break;
				case 1:
					if (!leftExistPoint && !rightExistPoint) {
						return !idUser ? 35 : 40;
					} else if (!leftExistPoint || !rightExistPoint) {
						return !idUser ? 25 : 30;
					}
					break;
			}
			return 0;
		}
	}
}
